package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;

@WebServlet("/PeajeHandler")
public class PeajeHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	private static String ADD = "/PeajeAdd.jsp";
    private static String Edit = "/Peajeedit.jsp";
    private static String PeajeRecord="/ListPeaje.jsp";
    private PeajeDao dao;
   
    public PeajeHandler() {
        super();
        dao = new PeajeDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listPeaje")) {
			redirect = PeajeRecord;
			request.setAttribute("Peaje", dao.getAllPeajes());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("PeajeId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Peaje objPeaje = new Peaje();
			objPeaje.setNombre_Peaje(request.getParameter("Nombre_Peaje"));
			objPeaje.setDepartamento(request.getParameter("Departamento"));
			objPeaje.setMunicipio(request.getParameter("Municipio"));
			dao.addPeaje(objPeaje);
			redirect = PeajeRecord;
			request.setAttribute("Peaje", dao.getAllPeajes());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String Peaje_Id = request.getParameter("PeajeId");
			int lugid = Integer.parseInt(Peaje_Id);
			dao.removePeaje(lugid);
			redirect = PeajeRecord;
			request.setAttribute("Peaje", dao.getAllPeajes());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String Peaje_Id = request.getParameter("PeajeId");
			int pegid = Integer.parseInt(Peaje_Id);
			Peaje objPeaje = new Peaje();
			objPeaje.setPeajeId(pegid);
			objPeaje.setNombre_Peaje(request.getParameter("Nombre_Peaje"));
			objPeaje.setDepartamento(request.getParameter("Departamento"));
			objPeaje.setMunicipio(request.getParameter("Municipio"));
			request.setAttribute("Peaje", objPeaje);
			redirect = PeajeRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listPeaje")) {
			redirect = PeajeRecord;
			request.setAttribute("Peaje", dao.getAllPeajes());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
